from .http_utils import *
import json


def get_audit_person_list():
    return http_get('/finance/getAuditList')


def finance_offline_order_list(param):
    # param = {'lineSize': page}
    r = http_post_json("/finance/offlineOrder/list", param)
    print(param)
    print(r)
    return r


def finance_create_receipt_bill(param=None):
    """
    生成对账单
    :param param:
        areaList	区域集合	body	false	array
        areas	区域集合	body	false	array
        billId	账单id	body	false	string
        createEndTime	订单/退货单创建结束时间	body	false	date-time
        createStartTime	订单/退货单创建开始时间	body	false	date-time
        customerId	报价负责人id	body	false	string
        garageName	修理厂名	body	false	string
        invoiceApplyStatus	发票申请状态	body	false	int32
        isConnect	是否关联账单	body	false	int32
        isReceipt	是否收款	body	false	int32
        orderId	主订单ID	body	false	string
        orderOutEndTime	搜索订单出库结束时间	body	false	date-time
        orderOutStartTime	搜索订单出库开始时间	body	false	date-time
        overDateStatus	逾期情况	body	false	int32
        page	当前页	body	false	int32
        pageSize	每页展示条数	body	false	int32
        payWay	支付方式 0 账期 3 货到付款	body	false	int32
        salesId	销售负责人id	body	false	string
        stockInOutEndTime	订单/退货单出入库时间	body	false	date-time
        stockInOutStartTime	订单/退货单出入库时间	body	false	date-time
    :return:
    """

    if param is None:
        param = {}

    print("生成对账单")
    param_bill = {"ids": []}
    garaga_id = ''
    page = 1
    ids = []
    while len(ids) == 0:
        param['page'] = page
        _data = finance_offline_order_list(param)['data']
        if len(_data['records']) == 0:
            raise RuntimeError('不存在待结算')
        olist = _data['records']

        for index in olist:
            if index['billNumber']:
                continue
            if not garaga_id and index['garageId']:
                garaga_id = index['garageId']

            if garaga_id == index['garageId'] and index['garageId']:
                ids.append(index['orderId'])
        page = page + 1

    param_bill['ids'] = ids
    print(json.dumps(param_bill, ensure_ascii=False))

    return http_post_json("/finance/offlineOrder/createReceiptBill", param_bill)


def receipt_bill_to_client():
    param = {"billId": []}
    return http_get("/finance/financeBillInfo/billList")


def finance_send_receipt_bill(bill_id):
    """
    发送对账单
    :param bill_id:
    :return:
    """
    print("发送对账单：", bill_id)
    param = {"billId": bill_id, "operationType": 1}
    return http_post_json("/finance/offlineOrder/sendReceiptBill", param)


def finance_client_check_bill(bill_id):
    """
    确认对账单
    :param bill_id:
    :return:
    """
    print("确认对账单：", bill_id)
    param = {"billId": bill_id, "operationType": 2}
    return http_post_json("/finance/offlineOrder/clientCheckBill", param)


def finance_bill_audit(bill_id, operation_type):
    """
    待对账情况下，财务审核队长
    :param bill_id: 账单号
    :param operation_type: 6 通过审核对账 11 重新对账，对账单变为待对账
    :return:
    """
    print("确认对账单：", bill_id, '操作类型：', operation_type)
    param = {"billId": bill_id, "operationType": operation_type}
    return http_post_json("/finance/offlineOrder/billAudit", param)


def finance_receipt_bill_detail(bill_id):
    """
    查询对账单详情
    :param bill_id: 账单号
    :return:
    """
    print("查询对账单详情：", bill_id)
    param = {"billId": bill_id}
    return http_post_json("/finance/offlineOrder/receiptBillDetail", param)


def finance_offline_order_bill_gather(bill_id, gather_amount):
    """
    对账单收款
    :param bill_id: 账单号
    :param gather_amount: 收款金额
    :return:
    """
    print("对账单收款：", bill_id, "收款金额：", gather_amount)
    param = {"billId": bill_id, "gatherAmount": gather_amount, "operationType": 5}
    return http_post_json("/finance/offlineOrder/billGather", param)


def finance_bill_audit_pass(bill_id):
    """
    对账单审核通过
    :param bill_id:
    :return:
    """
    print("确认对账单：", bill_id)
    param = {"billId": bill_id, "operationType": 2}
    return http_post_json("/finance/offlineOrder/billAudit", param)


def receipt_bill_detail(billId):
    param = {"billId": billId}
    return http_get_str("/finance/offlineOrder/receiptBillDetail", param)


def receipt_bill_detail1(billId):
    param = {"billId": billId}
    return http_get("/finance/offlineOrder/receiptBillToClient", param)


def finance_receipt_bill_send_mail(billId, email):
    param = {
        "billId": billId,
        "email": email
    }
    return http_post_json("http://localhost:8081/finance/sendBillToMail", param)


def finance_receipt_invoice_send_mail(invoice_id, email):
    param = {
        "invoiceId": invoice_id,
        "email": email
    }
    return http_post_json("http://localhost:8081/finance/invoice/sendInvoiceToEmail", param)
